package socdev.mobile;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.apache.http.NameValuePair;

import socdev.data.JSonDownloader;
import socdev.data.Project;
import socdev.data.Task;
import socdev.data.User;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class CreateTaskActivity extends Activity{
	
	static Task task;
	static Project project;
	static User user;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.create_task);
        
        project = (Project) getIntent().getExtras().get("project");
        user = (User) getIntent().getExtras().get("user");
        Button add = (Button) findViewById(R.id.add_button);
        add.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				try {
					EditText name = (EditText) findViewById(R.id.name);
					EditText description = (EditText) findViewById(R.id.description);
					EditText deadline = (EditText) findViewById(R.id.deadline);
				
					System.out.println(name.getText().toString());
					System.out.println(deadline.getText());
				
				
				
					
					SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy"); //please notice the capital M
				
					Date date = formatter.parse(deadline.getText().toString());
				
				
					task = new Task(name.getText().toString(), description.getText().toString(), date);
				
				
					Thread push = new Thread() {
						@Override
						public void run() {
							try {
        			
								JSonDownloader.postData("http://socdev-dev1.appspot.com/api/task/create/", task.getCreatNameValuePairs(project.getNameValuePairs(user.getNameValuePairs(new ArrayList<NameValuePair>()))));
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					};
					push.start();
					finish();
        		} catch (ParseException e1) {
        			TextView info = (TextView) findViewById(R.id.info);
        			info.setText("Špatný formát data");
					
				}
        		
        		
				
			}
            

        });
        
      
        
        
    }
}
